#include "edit.h"
#define SCI_LEXER // 启用 Lexer 支持
#include <dwmapi.h>
#include "SciLexer.h"
#include "Scintilla.h"
#include "ILexer.h"
#include "Lexilla.h"
#define IDC_GuiID 1001

#pragma comment(lib, "dwmapi.lib")

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

void DrawCustomTitlebar(HWND hwnd, HDC hdc);
static std::shared_ptr<edit::Editor> editor = std::make_shared<edit::Editor>();
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iShowCmd) {

    // 初始化时加载 DLL
    HMODULE hSciDLL = LoadLibrary(TEXT("Scintilla.dll"));
    if (!hSciDLL) {
        MessageBoxW(nullptr, TEXT(L"Scintilla.dll 加载失败！"), TEXT(L"错误"), MB_ICONERROR);
        return -1;
    }
    WNDCLASS wc = { 0 };
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = "ae";
    wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

    if (!::RegisterClass(&wc)) {
        return 0;
    }

    HWND hwnd = ::CreateWindowExA(NULL, 
        "ae", 
        "ae", 
        // WS_OVERLAPPEDWINDOW & ~WS_CAPTION, // 移除 WS_CAPTION 样式
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        nullptr, nullptr, hInstance, nullptr
    );

    // // 确保窗口边框扩展 (支持透明效果)
    // MARGINS margins = {0, 0, 0, 0}; // 将标题栏区域扩展到客户区
    // ::DwmExtendFrameIntoClientArea(hwnd, &margins);

    ::ShowWindow(hwnd, iShowCmd);
    ::UpdateWindow(hwnd);

    MSG msg;
    while (::GetMessage(&msg, nullptr, 0, 0)) {
        ::TranslateMessage(&msg);
        ::DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_CREATE: {
            editor->Init(((LPCREATESTRUCT)lParam)->hInstance, hwnd);
            // GetModuleHandle(nullptr)
            /*
            HWND hSci = CreateWindow("Scintilla",
                "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPCHILDREN,
                0, 0, 800, 600, hwnd, (HMENU)IDC_GuiID, ((LPCREATESTRUCT)lParam)->hInstance, NULL);

            if (!hSci) {
                MessageBoxW(hwnd, TEXT(L"Scintilla 控件创建失败！"), TEXT(L"错误"), MB_ICONERROR);
                return -1;
            }
            
            // ILexer5 *lexer = CreateLexer("cpp");
            // 初始化 Scintilla
            SendMessage(hSci, SCI_SETCODEPAGE, SC_CP_UTF8, 0); // 设置 UTF-8 编码
            SendMessage(hSci, SCI_SETMARGINWIDTHN, 0, 50);     // 显示行号边栏
            // SendMessage(hSci, SCI_SETILEXER, SCLEX_CPP, 0);     // 设置 C++ 语法高亮
            Scintilla::ILexer5 *pLexer = ::CreateLexer("cpp");
            // CallScintilla(scintilla, SCI_SETILEXER, 0, pLexer);
            // SCI_SETILEXER();
            SendMessage(hSci, SCI_SETILEXER, 0, reinterpret_cast<LPARAM>(pLexer));     // 设置 C++ 语法高亮
            // SCI_SETILEXER();
            */
            break;
        }
        // case WM_CHAR:
        //     MessageBoxA(hwnd, "WM_CHAR received!", "Debug", MB_OK);
        //     return 0;
        case WM_DESTROY:
            ::PostQuitMessage(0);
            return 0;
        /*
        case WM_NCCALCSIZE: {
            // 移除非客户区 (标题栏和边框)
            if (wParam == TRUE) {
                NCCALCSIZE_PARAMS* params = (NCCALCSIZE_PARAMS*)lParam;
                return 0;
            }
            break;
        }
        case WM_ACTIVATE: {
            // 启用 DWM 模糊效果 (可选)
            BOOL enable = TRUE;
            ::DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &enable, sizeof(enable));
            break;
        }
        case WM_PAINT: {
            // 绘制自定义标题栏
            PAINTSTRUCT ps;
            HDC hdc = ::BeginPaint(hwnd, &ps);
            DrawCustomTitlebar(hwnd, hdc);
            ::EndPaint(hwnd, &ps);
            break;
        }*/
        default: {
            return ::DefWindowProc(hwnd, msg, wParam, lParam);
        }
    }

    return 0;
}

void DrawCustomTitlebar (HWND hwnd, HDC hdc) {
    // 绘制背景
    RECT rect;
    ::GetClientRect(hwnd, &rect);
    // 标题栏高度
    rect.bottom = 32; 
    ::FillRect(hdc, &rect, (HBRUSH)::GetStockObject(WHITE_BRUSH));

    // 绘制标题文字
    ::SetBkMode(hdc, TRANSPARENT);
    ::DrawTextA(hdc, "aeditor", -1, &rect, DT_LEFT | DT_VCENTER);

    // 绘制关闭按钮
    RECT btnRect = { rect.right - 32, 0, rect.right, 32 };
    ::DrawTextA(hdc, "X", -1, &btnRect, DT_RIGHT);
}